package com.csipsimple.phone;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.provider.CallLog;
import android.telephony.TelephonyManager;

/**
*
* Broadcast receiver that reacts to an incoming call 
*
* credit goes to the following open source projects 
* 
* teddsdroidtools - http://code.google.com/p/teddsdroidtools/
* auto-answer - http://code.google.com/p/auto-answer/		
* 
*
*
*/
public class PhoneCallReceiver extends BroadcastReceiver {
	
	
	public static final String SIP_ACTIVE="com.urnum.intent.action.SIP_ACTIVE";
	public static final String SIP_DEACTIVE="com.urnum.intent.action.SIP_DEACTIVE";
	public static final String SIP_NUMBER="com.urnum.intent.action.SIP_NUMBER";
	
	/**
	 * Call back which fires off when the phone changes state.  
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Intent i = new Intent(context, CallAnswerIntentService.class);
		i.putExtra("actionstring",action);
		if (action.equals(SIP_NUMBER)){
			String sipNumber=intent.getStringExtra("sipnumber");
			i.putExtra("sipnumber", sipNumber);
		}
		if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
			String phone_state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
			i.putExtra(TelephonyManager.EXTRA_STATE, phone_state);
			if (phone_state.equals(TelephonyManager.EXTRA_STATE_RINGING)){ 
				String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
				i.putExtra("number", number);
			}
		}

		context.startService(i);

	}
	 
		
}
	

	
		

	
